<?php

// +----------------------------------------------------------------------
// | author: admin
// +----------------------------------------------------------------------

namespace app\adminapi\logic\order;


use app\common\model\order\Order;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * Order逻辑
 * Class OrderLogic
 * @package app\adminapi\logic\order
 */
class OrderLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/29 17:52
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            Order::create([
                'order_no' => $params['order_no'],
                'order_type' => $params['order_type'],
                'user_id' => $params['user_id'],
                'people_num' => $params['people_num'],
                'baggage_num' => $params['baggage_num'],
                'line_id' => $params['line_id'],
                'driver_id' => $params['driver_id'],
                'car_num' => $params['car_num'],
                'estimated_time' => $params['estimated_time'],
                'actual_time' => $params['actual_time'],
                'start_address' => $params['start_address'],
                'end_address' => $params['end_address'],
                'way_address' => $params['way_address'],
                'people_name' => $params['people_name'],
                'people_phone' => $params['people_phone'],
                'verify_code' => $params['verify_code'],
                'pay_type' => $params['pay_type'],
                'pay_time' => $params['pay_time'],
                'price' => $params['price'],
                'order_status' => $params['order_status'],
                'refund_status' => $params['refund_status'],
                'refund_price' => $params['refund_price'],
                'refund_time' => $params['refund_time'],
                'cancel_type' => $params['cancel_type'],
                'cancel_desc' => $params['cancel_desc'],
                'cancel_time' => $params['cancel_time'],
                'cancel_reason' => $params['cancel_reason'],
                'complete_time' => $params['complete_time'],
                'complete_desc' => $params['complete_desc'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/29 17:52
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Order::where('id', $params['id'])->update([
                'order_no' => $params['order_no'],
                'order_type' => $params['order_type'],
                'user_id' => $params['user_id'],
                'people_num' => $params['people_num'],
                'baggage_num' => $params['baggage_num'],
                'line_id' => $params['line_id'],
                'driver_id' => $params['driver_id'],
                'car_num' => $params['car_num'],
                'estimated_time' => $params['estimated_time'],
                'actual_time' => $params['actual_time'],
                'start_address' => $params['start_address'],
                'end_address' => $params['end_address'],
                'way_address' => $params['way_address'],
                'people_name' => $params['people_name'],
                'people_phone' => $params['people_phone'],
                'verify_code' => $params['verify_code'],
                'pay_type' => $params['pay_type'],
                'pay_time' => $params['pay_time'],
                'price' => $params['price'],
                'order_status' => $params['order_status'],
                'refund_status' => $params['refund_status'],
                'refund_price' => $params['refund_price'],
                'refund_time' => $params['refund_time'],
                'cancel_type' => $params['cancel_type'],
                'cancel_desc' => $params['cancel_desc'],
                'cancel_time' => $params['cancel_time'],
                'cancel_reason' => $params['cancel_reason'],
                'complete_time' => $params['complete_time'],
                'complete_desc' => $params['complete_desc'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author admin
     * @date 2025/04/29 17:52
     */
    public static function delete(array $params): bool
    {
        return Order::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author admin
     * @date 2025/04/29 17:52
     */
    public static function detail($params): array
    {
        return Order::findOrEmpty($params['id'])->toArray();
    }
}